package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.model.HistoryListAdapter;
import com.example.model.HistoryModel;
import com.example.test.R;

public class FavoriteView extends Activity  {
	private ListView favList;
	private HistoryModel hisModel;
	private Cursor mCursor;
	private Button go,other,cancel;
	private TextView time,loc;
	private int History_ID = 0;
	private View itemView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite_view);
		
		
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String location = bundle.getString("CURLOC");
		String timeFrame=bundle.getString("TIMEFRAME");
		loc=(TextView)findViewById(R.id.text_test_loc);
		time=(TextView)findViewById(R.id.text_text_time);
		
		hisModel = new HistoryModel(this);//***********************
		mCursor = hisModel.sort(timeFrame, location);//*************************
		loc.setText(location);
		time.setText(timeFrame);
		
		
		

		mCursor = hisModel.fav(timeFrame, location);
		
		favList=(ListView)findViewById(R.id.list_fav);
		favList.setAdapter(new HistoryListAdapter(this,mCursor));
		favList.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				itemBackChanged(view);
				mCursor.moveToPosition(position);
				History_ID = mCursor.getInt(0);
		//		locName.setText(mCursor.getString(1));
		//		currentLoc.setText(mCursor.getString(2));
				
			}

			private void itemBackChanged(View view) {
				view.setBackgroundResource(android.R.color.holo_red_light);

				itemView = view;
				
			}
			
		});
		go=(Button)findViewById(R.id.btn_fav_go);
		go.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FavoriteView.this, BrowserView.class);
				Bundle bundle = new Bundle();
				String url=mCursor.getString(1);
				String curLoc = mCursor.getString(4);
				System.out.printf(url, curLoc);
				bundle.putString("CURLOC", curLoc);
				bundle.putString("URL", url);

				intent.putExtras(bundle);
				startActivity(intent);
				
			}
			
		});
		other=(Button)findViewById(R.id.btn_fav_other);
		other.setOnClickListener(new OnClickListener()
		{
			Intent intent = getIntent();
			Bundle bundle = intent.getExtras();
			String location = bundle.getString("CURLOC");
			

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FavoriteView.this, TopSiteView.class);
				Bundle bundle = new Bundle();
				
				bundle.putString("CURLOC", location);

				intent.putExtras(bundle);
				startActivity(intent);
				
			}
			
		});
		cancel=(Button)findViewById(R.id.btn_fav_cancel);
		cancel.setOnClickListener(new OnClickListener()
		{
			Intent intent = getIntent();
			Bundle bundle = intent.getExtras();
			String location = bundle.getString("CURLOC");

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FavoriteView.this, BrowserView.class);
                 Bundle bundle = new Bundle();
				
				bundle.putString("CURLOC", location);

				intent.putExtras(bundle);
				startActivity(intent);
			}
			
		});
		
		
	}
		

}
